package fussbudget;

import java.sql.ResultSet;
import java.sql.SQLException;

import fussbudget.Transaction.TYPE;
import fussbudget.SQL.FBSQL;

public class ScheduledTransaction {
	public enum RECURRANCE {
		ONCE,
		DAILY,
		WEEKLY,
		BI_WEEKLY,
		MONTHLY,
		BI_MONTHLY,
		SEASONALLY,
		HALF_YEARLY,
		YEARLY
	}

	private int					id;
	private String				name			= "New Sceduled Transaction";
	private RECURRANCE			recurrance		= RECURRANCE.MONTHLY;
	private SimpleTransaction	startTransaction;
	private PaymentEntity		paymentEntity	= PaymentEntitySet.undefinedEntity;
	private TYPE				type = TYPE.DEPOSIT;

	public ScheduledTransaction(String name) {
		this.name = name;
		startTransaction = new SimpleTransaction();
		id = FBSQL.addScheduledTransaction(this);
	}

	public ScheduledTransaction(ResultSet rs) {
		try {
			id = rs.getInt("id");
			name = rs.getString("name");
			recurrance = RECURRANCE.valueOf(rs.getString("recurrance"));

			int idStartTransaction = rs.getInt("idStartTransaction");
			startTransaction = new SimpleTransaction(FBSQL.getResultSet("simpleTransaction", "*", "id=" + idStartTransaction));

			int idPaymentEntity = rs.getInt("idPaymentEntity");
			paymentEntity = FussBudgetMain.allEntitySet.getEntityByID(idPaymentEntity);
			
			if(rs.getBoolean("isDeposit")){
				type = TYPE.DEPOSIT;
			}else{
				type = TYPE.PAYMENT;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public synchronized int getId() {
		return id;
	}

	public synchronized void setId(int id) {
		this.id = id;
	}

	public synchronized String getName() {
		return name;
	}

	public synchronized void setName(String name) {
		this.name = name;
		FBSQL.alterString("scheduledTransaction", "name", name, id);
	}

	public synchronized RECURRANCE getRecurrance() {
		return recurrance;
	}

	public synchronized void setRecurrance(String recurrance) {
		setRecurrance(RECURRANCE.valueOf(recurrance));
	}

	public synchronized void setRecurrance(RECURRANCE recurrance) {
		this.recurrance = recurrance;
		FBSQL.alterString("scheduledTransaction", "recurrance", recurrance.name(), id);
	}

	public synchronized SimpleTransaction getStartTransaction() {
		return startTransaction;
	}

	public synchronized void setStartTransaction(SimpleTransaction startTransaction) {
		this.startTransaction = startTransaction;
		FBSQL.alterInt("scheduledTransaction", "idStartTransaction", startTransaction.getId(), id);
	}

	public synchronized PaymentEntity getPaymentEntity() {
		return paymentEntity;
	}

	public synchronized void setPaymentEntity(PaymentEntity paymentEntity) {
		this.paymentEntity = paymentEntity;
		FBSQL.alterInt("scheduledTransaction", "idPaymentEntity", paymentEntity.getId(), id);
	}

	public synchronized TYPE getType() {
		return type;
	}

	public synchronized void setType(TYPE type) {
		this.type = type;
	}

}
